﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRMMis.BLL;
using System.Data;
using Ext.Net;

public partial class CRM_Store_ERP_InStoreAddCargo : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["ObjId"] = Request.QueryString["ObjId"];
            exthid_supplier.Text = Request.QueryString["supplier"];
            exthid_storeroom.Text = Request.QueryString["storeroomId"];
            StoreBind(Request.QueryString["jsontext"]);
        }
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        string querStr = GetQryString();
        int recordcount = ERP_Cargo_Detail.GetRecorderCount(querStr);
        int PageSize = extcomb_DetailId.PageSize;
        int CurPage = e.Start / PageSize + 1;
        e.Total = recordcount;
        DataTable tmpDT = ERP_Cargo_Detail.GetPageList(PageSize, CurPage, querStr, "CargoName,Spec,ProduceName,Area,Supplyprice").Tables[0];
        store.DataSource = tmpDT; 
        store.DataBind();
    }


    private string GetQryString()
    {
        string StrSql = "State=1 and SupplyId=" + exthid_supplier.Text;
        //StrSql += "and storeroom_id=" + exthid_storeroom.Text + " and supplier_id=" + exthid_supplier.Text;
        string cargoText = FilterSpecChar(extcomb_DetailId.SelectedItem.Text.Trim());
        if (!string.IsNullOrEmpty(cargoText))
            StrSql += "  and  ( CargoName like '%" + cargoText + "%'  or Spell like '%" + cargoText + "%' )";
        return StrSql;
    }

    private void StoreBind(string jsonText)
    {
        if (ViewState["ObjId"].ToString() != "Add")
        {
            extcomb_DetailId.ReadOnly = true;
            extcomb_DetailId.StyleSpec = "background:#fffbf0;";

            Button2.Hidden = true;
            Dictionary<string, Object> obj = JSON.Deserialize<Dictionary<string, Object>>(jsonText);
            exttxtf_BatchNo.SetValue(obj["BatchNo"]);
            extnumf_Nums.SetValue(obj["NCOUNT"]);
            //exttxtf_UseNAME.SetValue(obj["UseNAME"]);
            exttxtf_ByName.SetValue(obj["ByName"]);
            extdate_AsepticDate.SetValue(obj["ASEPSIS_DATE"]);
            extspin_asepsis_limit.SetValue(obj["ASEPSIS_LIMIT"]);

            if (Convert.ToDateTime(obj["VALID_DATE"]) == new DateTime(2099, 1, 1))
            {
                disp_CQYX1.Hidden = false;
                extspin_valid_limit.Hidden = disp_text1.Hidden = extdate_ExpiryDate.Hidden = !disp_CQYX1.Hidden;
                CompositeField_valid_date.FieldLabel = "<input name='cbox' type='checkbox' onclick='check_valid_date(this);'  />有效期";
            }
            else
            {
                extdate_ExpiryDate.SetValue(obj["VALID_DATE"]);
                extspin_valid_limit.SetValue(obj["VALID_LIMIT"]);
                disp_CQYX1.Hidden = true;
                extspin_valid_limit.Hidden = disp_text1.Hidden = extdate_ExpiryDate.Hidden = !disp_CQYX1.Hidden;
            }
        }
    }

    [DirectMethod]
    public void SetCombCurrentnameSelect(string detail_id)
    {
        ViewState["detail_id"] = extcomb_DetailId.SelectedItem.Value;
        Hidden1.Value = ViewState["detail_id"].ToString();
        ShowCargoInfo(detail_id);
    }

    protected void Exttxtf_BatchNo_Change(object sender, DirectEventArgs e)                                 
    {
        bool isEmpty = string.IsNullOrEmpty(exttxtf_BatchNo.Text.Trim());
        string field = "<font color='red' >*</font>";

        extdate_ProduceDate.AllowBlank = isEmpty;
        extdate_ProduceDate.FieldLabel = isEmpty ? "生产日期" : field + "生产日期";

        extdate_DisinfectionDate.AllowBlank = isEmpty;
        extdate_DisinfectionDate.FieldLabel = isEmpty ? "消毒日期" : field + "消毒日期";

        extdate_ExpiryDate.AllowBlank = isEmpty;
        extdate_ExpiryDate.FieldLabel = isEmpty ? "有效期" : field + "有效期";

        extdate_AsepticDate.AllowBlank = isEmpty;
        extdate_AsepticDate.FieldLabel = isEmpty ? "无菌有效期" : field + "无菌有效期";

        Panel2.DoLayout();
    }

    private void ShowCargoInfo(string detailid)
    {
        DataTable dt = ERP_Cargo_Detail.GetList("DetailId=" + detailid).Tables[0];
        if (dt.Rows.Count > 0)
        {
            SetContainer(Container1, dt);
        }
    }

    private void ClearCargoInfo()
    {
        exttxtf_BatchNo.SetValue("");
        extdate_ProduceDate.SetValue("");
        extdate_DisinfectionDate.SetValue("");
        extdate_ExpiryDate.SetValue("12");
        extdate_AsepticDate.SetValue("12");
    }

    [DirectMethod]
    public void SaveInfo(string str)
    {
        string ptabid = Request.QueryString["ptabid"];
        ResourceManager1.AddScript("var p = parent.Ext.getCmp('CargoSelect');var obj = p.gobj;obj('" + str + "','" + ViewState["ObjId"].ToString() + "');Closepopwin2('CargoSelect');");
    }

    [DirectMethod]
    public void CounSaveInfo(string str)
    {
        string ptabid = Request.QueryString["ptabid"];
        exttxtf_BatchNo.SetValue("");
        Exttxtf_BatchNo_Change(null, null);
        ResourceManager1.AddScript("var p = parent.Ext.getCmp('CargoSelect');var obj = p.gobj;obj('" + str + "','" + ViewState["ObjId"].ToString() + "');");

    }

}
